package token.mobileapp.controlactivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import token.mobileapp.R;
import token.mobileapp.TokenActivity;
import token.mobileapp.model.UserInfo;
import token.mobileapp.persistenceactivity.UserInfoPersistence;
import token.mobileapp.utils.Constants;

import java.io.FileNotFoundException;

/**
 * Created by IntelliJ IDEA.
 * UserInfo: AVAVT
 * Date: 7/9/11
 * Time: 6:41 AM
 * To change this template use File | Settings | File Templates.
 */
public class Launcher extends TokenActivity {
    private UserInfo userInfo;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.launcher);
        Intent intent = new Intent(this, UserInfoPersistence.class);
        startActivityForResult(intent, Constants.REQUEST_CODE_LOAD_USER);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Constants.REQUEST_CODE_LOAD_USER && resultCode == Activity.RESULT_OK) {
            Bundle bundle = data.getExtras();
            userInfo = (UserInfo) bundle.getSerializable("userInfo");

            if(userInfo != null && userInfo.getPINCode() != null){
                Intent intent = new Intent(this, Login.class);
                startActivity(intent);
                this.finish();
            }
            else{
                Intent intent = new Intent(this, Initialization.class);
                startActivity(intent);
                this.finish();
            }
        } else if (requestCode == Constants.REQUEST_CODE_LOAD_USER && resultCode == Activity.RESULT_CANCELED) {
            if (data.getExtras().get("err") instanceof FileNotFoundException) {
                Intent intent = new Intent(this, Initialization.class);
                startActivity(intent);
                this.finish();
            } else {
                displayError(data);
            }
        }
    }
}

